﻿@{
    Layout = "~/Views/Shared/_Layout_List.cshtml";
}

<div class="fx-content">
    <div id="toobar">
        <div class="btn-group btn-group-sm">
            <button onclick="openForm('', '添加数据');" type="button" class="btn btn-default" aria-label="Left Align">
                <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
                添加
            </button>
            <button onclick="deleteData();" type="button" class="btn btn-default" aria-label="Right Align">
                <span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
                删除
            </button>
            <button type="button" class="btn btn-default" aria-label="Right Align" onclick="javascript: location.reload();">
                <span class="glyphicon glyphicon-refresh" aria-hidden="true"></span>
                刷新
            </button>
        </div>
    </div>
    <div class="fx-wrapper">
        <form class="form-inline" id="searchForm">
            <div class="form-group">
                <label>查询类别</label>
                <select class="selectpicker" name="condition" data-style="btn-default btn-sm" data-width="100px">
                    <option value="">请选择</option>
                    <option value="RoleName">角色名</option>
                </select>
                <input type="text" class="form-control input-sm" name="keyword" placeholder="请输入关键字">
            </div>
            <div class="form-group">
                <button type="button" class="btn btn-default btn-sm" onclick="javascript: $('#dataTable').bootstrapTable('refresh', { silent: true });">
                    <i class="glyphicon glyphicon-search"></i>
                    查询
                </button>
            </div>
        </form>
    </div>
    <div class="fx-wrapper">
        <table id="dataTable" class="table-bordered"></table>
    </div>
</div>

<script>
    var $table = $('#dataTable');

    $(function () {
        initTable();
        bindEvent();
    });

    //初始化表格
    function initTable() {
        $table.bootstrapTable({
            url: '/Base_SysManage/Base_SysRole/GetDataList',
            idField: 'Id',
            pagination: true,
            method: 'post',
            clickToSelect: false,
            sidePagination: "server",
            pageNumber: 1,
            pageSize: 30,
            pageList: [10, 30, 60, 100],
            columns: [
                { title: 'ck', field: 'ck', checkbox: true, width: '3%' },
                { title: '角色名', field: 'RoleName', width: '10%' },
                {
                    title: '操作', field: '_', width: '80%', formatter: function (value, row) {
                        var builder = new BtnBuilder();
                        builder.AddBtn({ icon: 'glyphicon-edit', function: 'openForm', param: [row['Id']] });
                        builder.AddBtn({ icon: 'glyphicon-trash', function: 'deleteData', param: [row['Id']] });
                        builder.AddBtn({ name: '权限分配', icon: 'glyphicon-edit', function: 'setPermission', param: [row['RoleId'], row['RoleName']] });

                        return builder.build();
                    }
                }
            ],
            queryParams: function (params) {
                var searchParams = $('#searchForm').getValues();
                $.extend(params, searchParams);

                return params;
            }
        });
    }

    //绑定事件
    function bindEvent() {

    }

    //打开表单
    function openForm(id, title) {
        dialogOpen({
            id: 'form',
            title: title,
            height:'30%',
            btn: ['确定', '取消'],
            url: '/Base_SysManage/Base_SysRole/Form?id={0}'.format(id || ''),
            yes: function (window, body) {
                window.submitForm();
            }
        });
    }

    //删除数据
    function deleteData(id) {
        dialogComfirm('确认删除吗？', function () {
            var ids = [];

            if (typeof (id) == 'string') {//单条数据
                ids.push(id);
            } else {//多条数据
                var rows = $table.bootstrapTable('getSelections');
                if (rows.length == 0) {
                    dialogError('请选择需要删除的数据！');
                    return;
                } else {
                    $.each(rows, function (index, value) {
                        ids.push(value['Id']);
                    })
                }
            }

            loading();
            $.postJSON('/Base_SysManage/Base_SysRole/DeleteData', { ids: JSON.stringify(ids) }, function (resJson) {
                loading(false);

                if (resJson.Success) {
                    $table.bootstrapTable('refresh');
                    dialogSuccess('删除成功!');
                }
                else {
                    dialogError(resJson.Msg);
                }
            });
        });
    }

    //设置权限
    function setPermission(roleId, roleName) {
        dialogOpen({
            id: 'permissionForm',
            title: '设置角色【{0}】权限'.format(roleName),
            width: '80.5%',
            height: '80.5%',
            btn: ['确定', '取消'],
            url: '/Base_SysManage/Base_SysRole/PermissionForm?roleId={0}'.format(roleId),
            yes: function (iframeWin) {
                iframeWin.savePermission();
            }
        });
    }
</script>
